define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT05DownloadService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            this.init = function(data,call_back){
                openDialog(data,call_back)
            };

            function openDialog(data,call_back){
                $mdDialog.dialog(
                    'module/moduleQT/program/QT05/dialog/QT05_download.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/QT05/QT05.css',
                            list: $filter('filter')(data.list, {visible:true}),
                            lot_code: data.lot_code,
                            init(){
                            },
                            back(){
                                dialog.list.forEach(element => {
                                    element.is_download = false;
                                });

                                dialog.hide();
                            },
                            confirm(){
                                if (dialog.isDisabled()) return
                                var content = {
                                    subset_query_conditon: []
                                };
                                dialog.list.forEach(function(panel) {
                                    panel.is_download && content.subset_query_conditon.push({
                                        mode_type: panel.panel_code,
                                        lot_code: dialog.lot_code,
                                        is_download: true
                                    })
                                });

                                if(window.nodeRequire){
                                    var remoteDialog = window.nodeRequire('electron').remote.dialog;
                                    remoteDialog.showOpenDialog({
                                        properties: ['openDirectory']
                                    },function(fileList){
                                        // 後端服務待修正
                                        return 
                                        if(fileList && fileList.length>0){
                                            $rootScope.showToast($filter('translate')('common.msg.file_downloading'));
                                            qualityTraceInfoGet(content, function(data) {
                                                var file_data = Buffer.from(data.file_data, 'base64');
                                                var file_name = fileList[0]+'\\'+$scope.title+moment().format('YYYYMMDDHHmmss')+'.xls';
                                                window.nodeRequire('fs').writeFile(file_name, file_data, function(error){
                                                    if(error){
                                                        $rootScope.showAlert($filter('translate')('common.msg.cantDownloadFile')+error);
                                                    } else {
                                                        $mdlToast.hide();
                                                        $rootScope.showConfirm($filter('translate')('common.msg.file_download_complete_and_open'), function(dialog){
                                                            dialog.hide();
                                                            window.nodeRequire('electron').shell.openItem(file_name);
                                                        });
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                                dialog.list.forEach(element => {
                                    element.is_download = false;
                                });
                                dialog.hide();
                            },
                            selectAll() {
                                let bool = true;
                                if(dialog.isSelectAll()) {
                                    bool = false;
                                }
                                dialog.list.forEach(element => {
                                    element.is_download = bool;
                                });
                            },
                            isSelectAll() {
                                return $filter('filter')(dialog.list, {is_download:true}).length === $filter('filter')(dialog.list, {visible:true}).length;
                            },
                            isDisabled() {
                                return $filter('filter')(dialog.list, {is_download:true}).length <= 0;
                            },
                            geItemTitle(code) {
                                return $filter('translate')(`QT05.sheet.${code}.title`);
                            }
                        };
                    }
                );
            };  
            
            
            var qualityTraceInfoGet = function (content,callback) 
            {
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.quality_trace_info_get',
                    content : content,
                    success : function(data)
                    {
                        callback && callback(data);
                    }
                });
            }
        }
    ])
});
